//
//  HomeViewController.m
//  KaneNote
//
//  Created by HuanJing on 2017/7/11.
//  Copyright © 2017年 HuanJing. All rights reserved.
//

#import "HomeViewController.h"
#import "SecondaryCatalogueController.h"
#import "WKWebViewController.h"

@interface HomeViewController ()

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSMutableArray *srcArray;

@end

static const NSInteger paddingW = 50;
static const NSInteger paddingH = 30;
static const NSInteger kBTN_TAG = 1200;

@implementation HomeViewController

- (NSArray *)titleArray {
    if (_titleArray == nil) {
        _titleArray = @[@"基础知识", @"角色职业", @"魔法技能",
                        @"物品装备", @"妖魔鬼怪"];
    }
    return _titleArray;
}

- (NSMutableArray *)srcArray {
    if (_srcArray == nil) {
        _srcArray = [NSMutableArray array];
    }
    return _srcArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    //网络状态改变会调用这个Block
    @weakify(self)
    self.networkChangedBlock = ^(NSInteger statue) {
        NSLog(@"networkStatue = %ld", statue);
        @strongify(self)
        if (statue != 0 && self.srcArray.count == 0) {
            [self initData];
        }
    };
}

- (void)fetchData {
    NSString *urlStr = [NSString stringWithFormat:@"%@/index.htm", HOST];
    NSLog(@"urlString = %@", urlStr);
    NSData *htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
    TFHpple *xPathParser = [[TFHpple alloc] initWithHTMLData:htmlData encoding:HTML_ENCODING];
    NSArray *elements = [xPathParser
        searchWithXPathQuery:@"//table[@width='1001']//td[@width='214']//tr"];
    for (NSInteger i = 2; i < elements.count-7; i++) {
        NSArray *eleAttriArr = [elements[i] children];
        //eleAtrriArr里面有三个Array, 其中只有第二个是完整的
        for (TFHppleElement *ele in [eleAttriArr[1] children]) {
//            NSLog(@"%@", [ele objectForKey:@"href"]);
            //保存网址到数组中
            [self.srcArray addObject:[ele objectForKey:@"href"]];
        }
    }
}

- (void)initUI {
    UIView *btnBgView = [[UIView alloc] init];
//    btnBgView.backgroundColor = [UIColor redColor];
    [self.view addSubview:btnBgView];
    [btnBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).
            insets(UIEdgeInsetsMake(kNavBar_Height +paddingH,
                                    paddingW,
                                    paddingH,
                                    paddingW));
    }];
    
    /**
     * 调用这个方法更新btnBgView的Frame
     * 之后才能获取到btnBgView的Frame
     */
    [self.view layoutIfNeeded];
    NSInteger titleCount = self.titleArray.count;
    CGFloat btnH = (btnBgView.frame.size.height - (titleCount + 1)*paddingH)/titleCount;
    CGFloat btnW = btnBgView.frame.size.width;
    for (NSInteger i = 0; i < self.titleArray.count; i++) {
        UIButton *childBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        childBtn.backgroundColor = [UIColor blueColor];
        [childBtn setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [childBtn setTitleColor:[UIColor whiteColor]
                       forState:UIControlStateNormal];
        childBtn.tag = kBTN_TAG + i;
        childBtn.frame = CGRectMake(0, (i+1)*paddingH + i*btnH, btnW, btnH);
        [btnBgView addSubview:childBtn];
        [childBtn addTarget:self
                     action:@selector(btnClick:)
           forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)btnClick:(UIButton *)btn {
    NSInteger tag = btn.tag - kBTN_TAG;
    //如果数组里面有东西, 才能跳转
    if (self.srcArray.count > 0) {
//        //祭坛那个不能跟别的一样解析
//        if (tag == self.srcArray.count-2) {
//            WKWebViewController *webVC = [[WKWebViewController alloc] init];
//            webVC.srcString = self.srcArray[tag];
//            [self.navigationController pushViewController:webVC animated:YES];
//            return;
//        }
        SecondaryCatalogueController *secondVC =
            [[SecondaryCatalogueController alloc] initWithSrc:self.srcArray[tag]];
        [self.navigationController pushViewController:secondVC animated:YES];
    } else {
        [MBProgressHUD showError:@"请先连接到网络"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
